package cn.wolfcode._IOday01_.synthesizeWork01;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;


public class InputStream00 {
    public static void main(String[] args) throws IOException {
        File file = new File("calcCharNum.txt");
        FileInputStream inputStream = new FileInputStream(file);
        //获取文件内容
        byte[] b = new byte[100] ;
        int off = 0 ;
        int len  ;
        String s = null;  //接收文件内容
        while ((len = inputStream.read(b)) != -1){
             s = new String(b, off, len);
            System.out.println(s);
        }

        //计文件D:/calcCharNum.txt中各个字母出现次数
        Map<Character , Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)){
                Integer integer = map.get(c);
                map.put(c,integer+1);
            }else {
                map.put(c,1);
            }
        }
        System.out.println(map);
        //迭代器遍历
        Set<Map.Entry<Character,Integer>> entries = map.entrySet();
        //Iterator<Map.Entry<Character,Integer>> iter = entries.iterator();
        /*while(iterator.hasNext()){
            Map.Entry<Character,Integer> i = iterator.next();
            System.out.println(i);
        }*/
        String str = "";  //接收数据拼接
        for (Map.Entry<Character,Integer> entry: entries){
            Character c = entry.getKey();
            Integer i = entry.getValue();
            str += c+"("+i+")"+",";
        }
        //去掉最后一个逗号
        System.out.println(str.substring(0, str.length() - 1));

    }
}
